package com.unitd.modules.configcenter.controller;

import com.unitd.frame.comm.utils.SimpleCryptUtils;
import com.unitd.frame.springweb.controller.result.AjaxResult;
import com.unitd.modules.comm.web.BaseController;
import com.unitd.modules.configcenter.component.CryptComponent;
import com.unitd.modules.configcenter.entity.AppSecret;
import com.unitd.modules.configcenter.entity.Profiles;
import com.unitd.modules.configcenter.service.IAppSecretService;
import com.unitd.modules.configcenter.service.IProfilesService;
import com.unitd.modules.sso.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @desc 配置中心应用加密信息管理前端控制器
 * @filename AppSecretController.java
 * @copyright www.unitd.com
 * @version 1.0
 * @author Hudan
 * @date 2018/1/31
 */
@RestController
@RequestMapping("/admin/appSecret")
public class AppSecretController extends BaseController {

	@Autowired
	private IAppSecretService appSecretService;
	@Autowired
	private IProfilesService profilesService;
	@Autowired
	private CryptComponent cryptComponent;

	/**
	 * @desc 给指定的app进行加密
	 * @param id 指定应用ID
	 * @return 自定义的通用json对象
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public AjaxResult appSecrets(@PathVariable("id") String id) {

		Map<String, List<AppSecret>> result = new HashMap<>(2);

		// 先获取所有的配置环境信息
		List<Profiles> profiles = profilesService.listAll();
		for (Profiles profile : profiles) {
			AppSecret appSecret = cryptComponent.getAppSecret(id, profile.getName(), "DES");
			appSecret.setSecretKey(appSecret.getSecretKey().substring(0, 28) + "****");
			result.put(profile.getName(), new ArrayList<>(Arrays.asList(appSecret)));
		}

		List<AppSecret> secrets = appSecretService.findByAppId(id, "RSA");

		for (AppSecret appSecret : secrets) {
			result.get(appSecret.getEnv()).add(appSecret);
		}

		return new AjaxResult(true, "加密成功!", result);
	}

	/**
	 * @desc 更新指定app的密钥
	 * @param params 更新参数
	 * @return 自定义的通用json对象
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public AjaxResult appSecretupdate(@RequestBody Map<String, String> params) {
		SecurityUtil.requireSuperAdmin();
		String appId = params.get("appId");
		String env = params.get("env");
		String secretKey = params.get("secretKey");
		String secretPass = params.get("secretPass");

		AppSecret secretEntity = appSecretService.selectByBean(new AppSecret(appId, env, "RSA"));
		if (secretEntity == null) {
			secretEntity = new AppSecret();
			secretEntity.setAppId(appId);
			secretEntity.setEnv(env);
			secretEntity.setSecretType("RSA");
			secretEntity.setSecretKey(secretKey);
			secretEntity.setSecretPass(SimpleCryptUtils.encrypt(secretKey, secretPass));
			appSecretService.insertSelective(secretEntity);
		} else {
			if (!secretKey.equals(secretEntity.getSecretKey()) || !secretPass.equals(secretEntity.getSecretPass())) {
				secretEntity.setSecretKey(secretKey);
				secretEntity.setSecretPass(SimpleCryptUtils.encrypt(secretKey, secretPass));
				appSecretService.updateByPrimaryKey(secretEntity);
			}
		}

		return new AjaxResult(true, "更新密钥成功!");
	}
}